import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router'
import HomeView from '@/views/HomeView.vue'
import ForumView from '@/views/ForumView.vue'
import ForumCategoryView from '@/views/ForumCategoryView.vue'
import QuestionSearchView from '@/views/QuestionSearchView.vue'
import QuestionCategoryView from '@/views/QuestionCategoryView.vue'
import StudyGroupsView from '@/views/StudyGroupsView.vue'
import ResourcesView from '@/views/ResourcesView.vue'
import AiSolutionView from '@/views/AiSolutionView.vue'
import AiChatView from '@/views/AiChatView.vue'
import LoginView from '@/views/LoginView.vue'
import RegisterView from '@/views/RegisterView.vue'
import UserProfileView from '@/views/UserProfileView.vue'
import AdminDashboardView from '@/views/admin/AdminDashboardView.vue'
import AdminUsersView from '@/views/admin/AdminUsersView.vue'
import AdminForumView from '@/views/admin/AdminForumView.vue'
import AdminQuestionsView from '@/views/admin/AdminQuestionsView.vue'
import AdminGroupsView from '@/views/admin/AdminGroupsView.vue'
import AdminStatisticsView from '@/views/admin/AdminStatisticsView.vue'
import NotFoundView from '@/views/NotFoundView.vue'
import ArticleDetailView from '@/views/ArticleDetailView.vue'
import PostDetailView from '@/views/PostDetailView.vue'
// 其他功能页面
import RetryMistakeView from '@/views/RetryMistakeView.vue'
import KnowledgeNotesView from '@/views/KnowledgeNotesView.vue'
import KnowledgePracticeView from '@/views/KnowledgePracticeView.vue'
import MockExamView from '@/views/MockExamView.vue'
import ResultAnalysisView from '@/views/ResultAnalysisView.vue'
import ErrorQuestionView from '@/views/ErrorQuestionView.vue'
import SpecialTrainingView from '@/views/SpecialTrainingView.vue'
// 用户中心页面
import AccountSettingsView from '@/views/AccountSettingsView.vue'
import CollectionView from '@/views/CollectionView.vue'
import BrowseHistoryView from '@/views/BrowseHistoryView.vue'
import { useUserStore } from '@/store/user'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
    meta: {
      title: '首页 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/forum',
    name: 'forum',
    component: ForumView,
    meta: {
      title: '论坛 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/forum/hot',
    name: 'forumHot',
    component: ForumView,
    meta: {
      title: '热门话题 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/article/:id',
    name: 'articleDetail',
    component: ArticleDetailView,
    meta: {
      title: '文章详情 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/forum/post/:id',
    name: 'postDetail',
    component: PostDetailView,
    meta: {
      title: '帖子详情 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/create-post',
    name: 'createPost',
    component: () => import('@/views/CreatePostView.vue'),
    meta: {
      title: '发布帖子 - GovernTalk公考论坛',
      layout: 'default',
      requiresAuth: true
    }
  },
  {
    path: '/group/:id',
    name: 'groupDetail',
    component: () => import('@/views/GroupDetailView.vue'),
    meta: {
      title: '小组详情 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/resource/:id',
    name: 'resourceDetail',
    component: () => import('@/views/ResourceDetailView.vue'),
    meta: {
      title: '资源详情 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/question/:id',
    name: 'questionDetail',
    component: () => import('@/views/QuestionDetailView.vue'),
    meta: {
      title: '题目详情 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/search-question',
    name: 'search-question',
    component: QuestionSearchView,
    meta: {
      title: '题目搜索 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/question-detail/:id',
    name: 'question-detail',
    component: () => import('@/views/QuestionDetailView.vue'),
    meta: {
      title: '题目详情 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/online-practice',
    name: 'online-practice',
    component: () => import('@/views/OnlinePracticeView.vue'),
    meta: {
      title: '在线练习 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/study-groups',
    name: 'study-groups',
    component: StudyGroupsView,
    meta: {
      title: '学习小组 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/resources',
    name: 'resources',
    component: ResourcesView,
    meta: {
      title: '学习资源 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/ai-solution',
    name: 'ai-solution',
    component: AiSolutionView,
    meta: {
      title: 'AI解题 - GovernTalk公考论坛',
      layout: 'default',
      requiresAuth: false
    }
  },
  {
    path: '/ai-chat',
    name: 'ai-chat',
    component: AiChatView,
    meta: {
      title: 'AI智能解题助手 - GovernTalk公考论坛',
      layout: 'default',
      requiresAuth: false
    }
  },
  {
    path: '/login',
    name: 'login',
    component: LoginView,
    meta: {
      title: '登录 - GovernTalk公考论坛',
      layout: 'auth',
      requiresGuest: true
    }
  },
  {
    path: '/register',
    name: 'register',
    component: RegisterView,
    meta: {
      title: '注册 - GovernTalk公考论坛',
      layout: 'auth',
      requiresGuest: true
    }
  },
  {
    path: '/profile/:id?',
    name: 'profile',
    component: UserProfileView,
    meta: {
      title: '个人中心 - GovernTalk公考论坛',
      layout: 'default',
      requiresAuth: false
    }
  },
  {
    path: '/user-profile',
    name: 'userProfileRedirect',
    redirect: '/profile',
    meta: {
      requiresAuth: false
    }
  },
  // 管理员路由
  {
    path: '/admin',
    name: 'admin',
    component: AdminDashboardView,
    meta: {
      title: '管理后台 - GovernTalk公考论坛',
      layout: 'admin',
      requiresAuth: false,
      requiresAdmin: false
    }
  },
  {
    path: '/admin/users',
    name: 'admin-users',
    component: AdminUsersView,
    meta: {
      title: '用户管理 - GovernTalk公考论坛',
      layout: 'admin',
      requiresAuth: false,
      requiresAdmin: false
    }
  },
  {
    path: '/admin/forum',
    name: 'admin-forum',
    component: AdminForumView,
    meta: {
      title: '论坛管理 - GovernTalk公考论坛',
      layout: 'admin',
      requiresAuth: false,
      requiresAdmin: false
    }
  },
  {
    path: '/admin/questions',
    name: 'admin-questions',
    component: AdminQuestionsView,
    meta: {
      title: '题库管理 - GovernTalk公考论坛',
      layout: 'admin',
      requiresAuth: false,
      requiresAdmin: false
    }
  },
  {
    path: '/admin/groups',
    name: 'admin-groups',
    component: AdminGroupsView,
    meta: {
      title: '小组管理 - GovernTalk公考论坛',
      layout: 'admin',
      requiresAuth: false,
      requiresAdmin: false
    }
  },
  {
    path: '/admin/statistics',
    name: 'admin-statistics',
    component: AdminStatisticsView,
    meta: {
      title: '数据统计 - GovernTalk公考论坛',
      layout: 'admin',
      requiresAuth: false,
      requiresAdmin: false
    }
  },
  // 学习功能页面
  {
    path: '/retry-mistake',
    name: 'retry-mistake',
    component: RetryMistakeView,
    meta: {
      title: '错题重做 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/knowledge-notes',
    name: 'knowledge-notes',
    component: KnowledgeNotesView,
    meta: {
      title: '知识笔记 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/knowledge-practice',
    name: 'knowledge-practice',
    component: KnowledgePracticeView,
    meta: {
      title: '专项练习 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/mock-exam',
    name: 'mock-exam',
    component: MockExamView,
    meta: {
      title: '模拟考试 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/result-analysis',
    name: 'result-analysis',
    component: ResultAnalysisView,
    meta: {
      title: '考试分析 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/error-question',
    name: 'error-question',
    component: ErrorQuestionView,
    meta: {
      title: '错题本 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  {
    path: '/special-training',
    name: 'special-training',
    component: SpecialTrainingView,
    meta: {
      title: '特训营 - GovernTalk公考论坛',
      layout: 'default'
    }
  },
  
  // 404页面
  // 用户中心页面
  { path: '/settings', name: 'settings', component: AccountSettingsView, meta: { title: '账号设置 - GovernTalk公考论坛', layout: 'default' } },
  { path: '/collections', name: 'collections', component: CollectionView, meta: { title: '我的收藏 - GovernTalk公考论坛', layout: 'default' } },
  { path: '/history', name: 'history', component: BrowseHistoryView, meta: { title: '浏览历史 - GovernTalk公考论坛', layout: 'default' } },
  
  // 论坛相关页面
  { path: '/forum/hot', name: 'forum-hot', component: ForumView, meta: { title: '热门话题 - GovernTalk公考论坛', layout: 'default' } },
  { path: '/forum/categories', name: 'forum-categories', component: ForumCategoryView, meta: { title: '论坛分类 - GovernTalk公考论坛', layout: 'default' } },
  { path: '/forum/create', name: 'forum-create', redirect: '/create-post' },
  
  // 题库相关页面
  { path: '/search-question/categories', name: 'question-categories', component: QuestionCategoryView, meta: { title: '题目分类 - GovernTalk公考论坛', layout: 'default' } },
  
  // 学习小组相关页面
  { path: '/study-groups/my', name: 'my-groups', component: StudyGroupsView, meta: { title: '我的小组 - GovernTalk公考论坛', layout: 'default' } },
  { path: '/study-groups/create', name: 'create-group', component: StudyGroupsView, meta: { title: '创建小组 - GovernTalk公考论坛', layout: 'default' } },
  
  // 学习资源相关页面
  { path: '/resources/categories', name: 'resource-categories', component: ResourcesView, meta: { title: '资源分类 - GovernTalk公考论坛', layout: 'default' } },
  { path: '/resources/upload', name: 'upload-resource', component: ResourcesView, meta: { title: '上传资源 - GovernTalk公考论坛', layout: 'default' } },
  { path: '/resources/hot', name: 'hot-resources', component: ResourcesView, meta: { title: '热门资源 - GovernTalk公考论坛', layout: 'default' } },
  
  // 404页面
  { path: '/:pathMatch(.*)*', name: 'not-found', component: NotFoundView, meta: { title: '页面不存在 - GovernTalk公考论坛', layout: 'default' } }
]

const router = createRouter({
  history: createWebHistory('/'),
  routes,
  scrollBehavior(_to, _from, savedPosition) {
    // 返回到之前的滚动位置
    if (savedPosition) {
      return savedPosition
    }
    // 滚动到顶部
    return { top: 0 }
  }
})

// 路由守卫
router.beforeEach((to, _from, next) => {
  // 设置页面标题
  document.title = (to.meta.title as string) || 'GovernTalk公考论坛'

  const userStore = useUserStore()
  const isAuthenticated = userStore.isAuthenticated
  const isAdmin = userStore.isAdmin

  // 检查是否需要登录
  if (to.meta.requiresAuth && !isAuthenticated) {
    next({ name: 'login', query: { redirect: to.fullPath } })
    return
  }

  // 检查是否需要管理员权限
  if (to.meta.requiresAdmin && !isAdmin) {
    next({ name: 'home' })
    return
  }

  // 检查是否需要访客状态
  if (to.meta.requiresGuest && isAuthenticated) {
    next({ name: 'home' })
    return
  }

  next()
})

export default router
